package com.netrust.cmsx.admin.modules.sys.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.netrust.cmsx.admin.modules.sys.service.RoleService;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.page.PageKey;
import com.netrust.cmsx.core.page.jq.JqGrid;
import com.netrust.cmsx.core.page.jq.JqPost;
import com.netrust.cmsx.core.page.jq.convert.Page2JQ;
import com.netrust.cmsx.core.resp.RespJson;
import com.netrust.cmsx.core.web.BaseController;
import com.netrust.cmsx.entity.module.sys.Role;


/**
 * 用户Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/role")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;
	
	@ResponseBody
	@RequestMapping(value = {""})
	public Role get(@RequestParam(required=false) String id) {
		if (id!=null){
			return roleService.findOne(id);
		}else{
			return new Role();
		}
	}
	
	@RequestMapping(value = {"form"})
	public String form(@RequestParam(required=false) String id,Model model) {
		Role role=new Role();
		
		if(id!=null){//代表修改跳转
			role=roleService.findOne(id);
		}
		
		model.addAttribute("role", role);
		return "modules/sys/roleForm";
	}
	
	@RequestMapping(value = {"save"})
	public String save(Role role) {		
		try {
			roleService.saveRole(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "modules/sys/roleList";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = {"del"})
	public RespJson del(@RequestParam(required=true) String id) {
		try {
			roleService.deleteInFlag(new Role(id));
			return new RespJson(Global.Operate_Success, 200);
		} catch (Exception e) {
			return new RespJson(Global.Operate_Error, 500, null);
		}
	}

	@ResponseBody
	@RequestMapping(value = {"list"})
	public JqGrid list(JqPost jqPost) {
		PageKey pk=new PageKey(null, jqPost.getPage(),jqPost.getRows());
		Page<Role> page =roleService.findList(pk);
		JqGrid jg=new Page2JQ<Role>().run(page,new String[]{"name","enname","role_type","data_scope","is_sys","useable"});		
		return jg;
	}
}
